package com.tangye.massageshop.util;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 分页辅助类
 * @param <T>
 */
@Data
public class PageQuery<T> {
    private final static Integer DEFAULT_PAGE_NUM = 1;
    private final static Integer DEFAULT_PAGE_SIZE = 5;

    private Integer pageNo = DEFAULT_PAGE_NUM;
    private Integer pageSize = DEFAULT_PAGE_SIZE;
    private String sortBy = "age";// 排序字段，默认使用 age
    private Boolean isAsc = true;// 排序方式，默认升序

    public Page<T> toMpPage(String sortBy, Boolean isAsc, Integer pageNo, Integer pageSize) {
        if (!StringUtils.isBlank(sortBy)) {
            this.sortBy = sortBy;
        }
        if (!Objects.isNull(isAsc)) {
            this.isAsc = isAsc;
        }
        if (!Objects.isNull(pageNo)) {
            this.pageNo = pageNo;
        }
        if (!Objects.isNull(pageSize)) {
            this.pageSize = pageSize;
        }
        Page<T> page = new Page<>(this.pageNo, this.pageSize);
        OrderItem orderItem = new OrderItem();
        orderItem.setAsc(this.isAsc);
        orderItem.setColumn(this.sortBy);
        page.addOrder(orderItem);
        return page;
    }
}
